package com.ruoyi.web.sso.api;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.core.config.TokenConfig;
import com.ruoyi.web.sso.common.util.ErrorCode;
import com.ruoyi.web.sso.common.util.*;
import com.ruoyi.web.sso.domain.*;
import com.ruoyi.web.sso.domain.DTO.*;
import com.ruoyi.web.sso.domain.vo.AreaVO;
import com.ruoyi.web.sso.domain.vo.Login;
import com.ruoyi.web.sso.domain.vo.RedisDetails;
import com.ruoyi.web.sso.domain.vo.StaffDetails;
import com.ruoyi.web.sso.mapper.GroupUserMapper;
import com.ruoyi.web.sso.mapper.StaffChildMapper;
import com.ruoyi.web.sso.service.*;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.ruoyi.common.config.datasource.DynamicDataSourceContextHolder.log;

@Controller
@RequestMapping("/api/sso/other_system")
@Api(tags = "Sso系统后台外部接口")
public class ApiSystemController {


    @Autowired
    private IRedisUtilService redisService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IPlatformService platformService;

    @Autowired
    private IAreaService areaService;

    @Autowired
    private IAreaStaffService areaStaffService;

    @Autowired
    private IStaffService staffService;

    @Autowired
    private IStaffRoleService staffRoleService;

    @Autowired
    private IDictDataService dictDataService;

    @Autowired
    private IGroupService groupService;

    @Autowired
    private GroupUserMapper groupUserMapper;

    @Autowired
    private IAppEditionService appEditionService;

    @Autowired
    private StaffChildMapper staffChildMapper;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "登录", notes = "登录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffDetails.class),
    })
    @ResponseBody
    public StatusResponse login(@RequestBody Login login, HttpServletRequest request) {
        return staffService.login(login, request);
    }


    @PostMapping(value = "/ids_by_staff_list", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "用户id集合查询角色对象集合", notes = "用户id集合查询角色对象集合")
    public StatusResponse getStaffRoleByStaffId(@ApiParam(value = "用户ID集合") @RequestParam(required = false) String[] staffIds) {
        String[] arr = ComUtil.getString(staffIds);
        return StatusResponse.success(staffService.getRoleByStaffIds(arr));
    }

    @PostMapping(value = "/get_all_area")
    @ResponseBody
    @TokenConfig(value = "1")
    //改成查询用户所属园区集合
    @ApiOperation(value = "查询用户所属园区集合", notes = "查询用户所属园区集合")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = Area.class),
    })
    public StatusResponse getStaffIdByArea(@ApiParam(value = "用户ID") @RequestParam(required = false) String staffId) {
        return StatusResponse.success(areaService.getAreasByStaffId(staffId));
    }

    @PostMapping(value = "/get_areas_by_ids")
    @ResponseBody
    //改成查询用户所属园区集合
    @ApiOperation(value = "查询用户所属园区集合", notes = "查询园区集合")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = Area.class),
    })
    public StatusResponse getAreaByIds(@ApiParam(value = "areaId") @RequestParam(required = false) String[] areaIds) {
        if (AssertUtil.isEmpty(areaIds)) {
            return StatusResponse.success(new ArrayList<>());
        }
        return StatusResponse.success(areaService.selectAreaByIds(areaIds));
    }

    @PostMapping(value = "/get_all_staff_by_areaId")
    @ResponseBody
    @TokenConfig(value = "1")
    @ApiOperation(value = "园区所有用户", notes = "园区所有用户")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = Staff.class),
    })
    public StatusResponse getAllStaffByAreaId(@ApiParam(value = "园区ID") @RequestParam(required = false) String areaId) {
        return StatusResponse.success(staffService.getAllStaffByAreaId(areaId, ""));
    }


    @PostMapping(value = "/home_info_charge_area")
    @ResponseBody
    @TokenConfig(value = "1")
    @ApiOperation(value = "查询用户详情，系统列表和园区列表", notes = "查询用户详情，系统列表和园区列表")
    public StatusResponse getDefaultStaffPlatform(@ApiParam(value = "用户ID") @RequestParam(required = false) String staffId) {
        Map map = new HashMap();

        map.put("staff", staffService.selectStaffById(staffId));
        map.put("platformList", platformService.selectStaffPlatform(staffId));
        map.put("areaList", areaService.getAreasByStaffId(staffId));

        return StatusResponse.success(map);
    }

    //园区员工数 [园区中除园长、副园长以为的员工数，离职不算在内]
    @PostMapping(value = "/get_area_staff_count")
    @ResponseBody
    @TokenConfig(value = "1")
    @ApiOperation(value = "园区员工数 [园区中除园长、副园长以为的员工数，离职不算在内]", notes = "园区员工数 [园区中除园长、副园长以为的员工数，离职不算在内]")
    public StatusResponse getAreaStaffCount(@ApiParam(value = "园区ID") @RequestParam(required = false) String areaId) {
        return StatusResponse.success(staffService.getAreaStaffCount(areaId));
    }

    @PostMapping(value = "/get_staff_position_list")
    @ResponseBody
    @TokenConfig(value = "1")
    @ApiOperation(value = "查询ticket用户的默认系统角色菜单信息", notes = "查询ticket用户的默认系统角色菜单信息-系统")
    public StatusResponse getStaffPositionList(@RequestHeader String ticket) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        return StatusResponse.success(menuService.selectSimpleMenuList(redisDetails.getStaffId(), redisDetails.getPlatformId()));
    }

    @TokenConfig(value = "1")
    @PostMapping(value = "/getDictData")
    @ResponseBody
    @ApiOperation(value = "字典标签获取字典集合", notes = "字典标签获取字典集合")
    public StatusResponse getDictData(@RequestHeader String ticket, DictData dictData) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        getIdDictData(redisDetails.getStaffId(), dictData);
        return StatusResponse.success(dictDataService.selectDictDataList(dictData));
    }

    @TokenConfig(value = "1")
    @PostMapping(value = "/dictCodeByDictData")
    @ResponseBody
    @ApiOperation(value = "字典code获取字典对象", notes = "字典code获取字典对象")
    public StatusResponse dictCodeByDictData(String dictCode) {
        return StatusResponse.success(dictDataService.selectDictDataByDictCode(dictCode));
    }


    @TokenConfig(value = "1")
    @PostMapping(value = "/addDictData")
    @ResponseBody
    @ApiOperation(value = "添加字典", notes = "添加字典")
    public StatusResponse addDictData(@RequestHeader String ticket, DictData dictData) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        getIdDictData(redisDetails.getStaffId(), dictData);
        return StatusResponse.success(dictDataService.insertAndUpdateDictData(dictData));
    }

    public void getIdDictData(String staffId, DictData dictData) {
        Area area = areaService.getArea(staffId);
        if (AssertUtil.isEmpty(area.getGroupId())) {
            throw new BusinessException(ErrorCode.UNDEFINED, "集团不存在");
        }
        if (!StringUtils.equals(dictData.getAreaId(), "0")) {
            dictData.setAreaId(area.getId());
        }
        if (!StringUtils.equals(dictData.getGroupId(), "0")) {
            if (AssertUtil.isNotEmpty(area.getGroupId())) {
                dictData.setGroupId(area.getGroupId());
            }
        }
    }

    @TokenConfig(value = "1")
    @PostMapping(value = "/delDictData")
    @ResponseBody
    @ApiOperation(value = "删除字典", notes = "删除字典")
    public StatusResponse delDictData(String dictCode) {

        if (AssertUtil.isEmpty(dictDataService.selectDictDataByDictCode(dictCode))) {
            StatusResponse.error(ErrorCode.STAFF_NOT_EXIST, "字典code不存在");
        }
        return StatusResponse.success(dictDataService.deleteDictDataByDictCode(dictCode));
    }

    @TokenConfig(value = "1")
    @PostMapping(value = "/updateDictData")
    @ResponseBody
    @ApiOperation(value = "修改字典", notes = "修改字典")
    public StatusResponse updateDictData(@RequestHeader String ticket, DictData dictData) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        getIdDictData(redisDetails.getStaffId(), dictData);
        return StatusResponse.success(dictDataService.insertAndUpdateDictData(dictData));
    }


    @PostMapping(value = "/get_ticket_by_staff", produces = "application/json;charset=utf-8")
    @ResponseBody
    @TokenConfig(value = "1")
    @ApiOperation(value = "ticket查询用户信息", notes = "ticket查询用户信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffDTO.class),
    })
    public StatusResponse getTicketByStaff(@RequestHeader String ticket) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        if (AssertUtil.isEmpty(redisDetails)) {
            return StatusResponse.error(ErrorCode.STAFF_NOT_EXIST, "ticket不存在");
        }
        return StatusResponse.success(staffService.selectStaffByIdSso(redisDetails.getStaffId()));
    }


    @PostMapping(value = "/get_ticket_by_staff_role", produces = "application/json;charset=utf-8")
    @ResponseBody
    @TokenConfig(value = "1")
    @ApiOperation(value = "ticket查询用户信息", notes = "ticket查询用户信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffDTO.class),
    })
    public StatusResponse getTicketByStaffRole(@RequestHeader String ticket) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        if (AssertUtil.isEmpty(redisDetails)) {
            return StatusResponse.error(ErrorCode.STAFF_NOT_EXIST, "ticket不存在");
        }
        return StatusResponse.success(staffService.selectStaffByIdSsoRole(redisDetails.getStaffId()));
    }


    @PostMapping(value = "/get_staff_id_by_staff", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "sso用户ID查询用户信息", notes = "sso用户ID查询用户信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffDTO.class),
    })
    public StatusResponse getStaffIdByStaff(String staffId) {
        Staff staff = staffService.selectStaffById(staffId);
        if (AssertUtil.isEmpty(staff)) {
            throw new BusinessException(ErrorCode.UNDEFINED, "用户ID不存在");
        }
        StaffDTO staffDTO = staffService.selectStaffByIdSso(staffId);
        return StatusResponse.success(staffDTO);
    }

    @PostMapping(value = "/getStaffIdByStaffJson", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "sso用户ID查询用户信息", notes = "sso用户ID查询用户信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffDTO.class),
    })
    public StatusResponse getStaffIdByStaffJson(String staffId) {
        Staff staff = staffService.selectStaffById(staffId);
        if (AssertUtil.isEmpty(staff)) {
            throw new BusinessException(ErrorCode.UNDEFINED, "用户ID不存在");
        }
        staff.setChildList(staffChildMapper.childList(staffId));
        String staffParam = JSON.toJSONString(staff);

        return StatusResponse.success(staffParam);
    }



    @PostMapping(value = "/get_mobile_by_staff", produces = "application/json;charset=utf-8")
    @ResponseBody
    @TokenConfig(value = "1")
    @ApiOperation(value = "sso用户手机号码查询用户信息", notes = "sso用户手机号码查询用户信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffDTO.class),
    })
    public StatusResponse selectMobileByStaff(String mobile) {
        Staff staff = staffService.selectMobileByStaff(mobile);
        if (AssertUtil.isEmpty(staff)) {
            throw new BusinessException(ErrorCode.UNDEFINED, "用户不存在");
        }
        StaffDTO staffDTO = staffService.selectMobileByStaffSso(mobile);
        return StatusResponse.success(staffDTO);
    }


    @PostMapping(value = "/update_staff", produces = "application/json;charset=utf-8")
    @ResponseBody
//    @TokenConfig(value = "1")
    @ApiOperation(value = "sso修改用户信息", notes = "sso修改用户信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffDTO.class),
    })
    public StatusResponse updateStaffSso(StaffDTO staffDTO) {
        Staff staff = staffService.selectStaffById(staffDTO.getId());
        if (AssertUtil.isEmpty(staff)) {
            throw new BusinessException(ErrorCode.UNDEFINED, "用户不存在");
        }
        if (StringUtils.equals(staffDTO.getAreaId(),"null") || AssertUtil.isEmpty(staffDTO.getAreaId())){
            staffDTO.setAreaId(staff.getAreaId());
        }


        String password = "";
        //当更改账号的时候
        if (!StringUtils.equals(staff.getMobile(),staffDTO.getMobile())){
            System.out.println("进入更改账号-------");
            String psw = PasswordUtil.encode("Abc@123456").toLowerCase();
            password = PasswordUtil.encode(staffDTO.getMobile() + psw + staff.getRaw());
            Staff staff1 = staffService.selectMobileByStaff(staffDTO.getMobile());
            if (AssertUtil.isNotEmpty(staff1)){
                throw new BusinessException(ErrorCode.UNDEFINED, "手机号已存在用户！");
            }
        }
        BeanUtils.copyProperties(staffDTO, staff);

        if (!StringUtils.equals(password,"")){
            staff.setPassword(password);
        }
        System.out.println("账号-------"+JSON.toJSONString(staff));
        staff.setGender(Integer.valueOf(staffDTO.getGender()));
        staffService.updateStaff(staff);
        return StatusResponse.success(staffService.selectStaffByIdSso(staffDTO.getId()));
    }




    @PostMapping(value = "/update_staff2", produces = "application/json;charset=utf-8")
    @ResponseBody
//    @TokenConfig(value = "1")
    @ApiOperation(value = "sso修改用户信息", notes = "sso修改用户信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffDTO.class),
    })
    public StatusResponse updateStaffSso2(StaffDTO staffDTO,String paramJson) {
        Staff staff = staffService.selectStaffById(staffDTO.getId());
        if (AssertUtil.isEmpty(staff)) {
            throw new BusinessException(ErrorCode.UNDEFINED, "用户不存在");
        }
        if (StringUtils.equals(staffDTO.getAreaId(),"null") || AssertUtil.isEmpty(staffDTO.getAreaId())){
            staffDTO.setAreaId(staff.getAreaId());
        }

        if(null!=staffDTO.getRoles() && staffDTO.getRoles().length>0){
            staff.setRoles(staffDTO.getRoles());
        }

        String password = "";
        //当更改账号的时候
        if (!StringUtils.equals(staff.getMobile(),staffDTO.getMobile())){
            System.out.println("进入更改账号-------");
            String psw = PasswordUtil.encode("Abc@123456").toLowerCase();
            password = PasswordUtil.encode(staffDTO.getMobile() + psw + staff.getRaw());
            Staff staff1 = staffService.selectMobileByStaff(staffDTO.getMobile());
            if (AssertUtil.isNotEmpty(staff1)){
                throw new BusinessException(ErrorCode.UNDEFINED, "手机号已存在用户！");
            }
        }
        BeanUtils.copyProperties(staffDTO, staff);

        if (!StringUtils.equals(password,"")){
            staff.setPassword(password);
        }
        System.out.println("账号-------"+JSON.toJSONString(staff));
        if (StringUtils.isNotEmpty(staffDTO.getGender())&&!staffDTO.getGender().equals("null")){
            staff.setGender(Integer.valueOf(staffDTO.getGender()));
        }
        staffService.updateStaff2(staff,paramJson);
        return StatusResponse.success(staffService.selectStaffByIdSso(staffDTO.getId()));
    }


//
//    @PostMapping(value = "/update_staff3", produces = "application/json;charset=utf-8")
//    @ResponseBody
////    @TokenConfig(value = "1")
//    @ApiOperation(value = "sso修改用户信息", notes = "sso修改用户信息")
//    @ApiResponses({
//            @ApiResponse(code = 200, message = "OK", response = StaffDTO.class),
//    })
//    public StatusResponse updateStaffSso3(StaffDTO staffDTO,String paramJson) {
//        if (staffDTO == null || StringUtils.isEmpty(staffDTO.getId())) {
//            throw new BusinessException(ErrorCode.UNDEFINED, "用户ID不能为空");
//        }
//        String staffId = staffDTO.getId();
//        Staff staff = staffService.selectStaffById(staffId);
//        //当更改账号的时候
//        if (!StringUtils.equals(staff.getMobile(),staffDTO.getMobile())){
//            System.out.println("进入更改账号-------");
//            Staff staff1 = staffService.selectMobileByStaff(staffDTO.getMobile());
//            if (AssertUtil.isNotEmpty(staff1)){
//                throw new BusinessException(ErrorCode.UNDEFINED, "手机号已存在用户！");
//            }
//        }
//        BeanUtils.copyProperties(staffDTO, staff);
//        staffService.updateStaff2(staff,paramJson);
//        return StatusResponse.success(staffService.selectStaffByIdSso(staffDTO.getId()));
//    }




    @PostMapping(value = "/update_staff3", produces = "application/json;charset=utf-8")
    @ResponseBody
    @TokenConfig(value = "1")
    @ApiOperation(value = "sso修改用户信息", notes = "sso修改用户信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffDTO.class),
    })
    public StatusResponse updateStaffSso3(StaffDTO staffDTO, String paramJson) {
        try {
            // 1. 创建ObjectMapper并配置
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

            // 2. 验证并解析paramJson
            if (StringUtils.isNotBlank(paramJson)) {
                // 移除可能的BOM头
                paramJson = paramJson.replace("\uFEFF", "").trim();

                // 验证JSON格式
                if (!isValidJson(paramJson)) {
                    throw new BusinessException(ErrorCode.UNDEFINED, "JSON参数格式无效");
                }

                // 解析JSON到临时DTO
                StaffDTO paramDto = objectMapper.readValue(paramJson, StaffDTO.class);

                // 3. 合并属性到主DTO
                BeanUtils.copyProperties(paramDto, staffDTO, getNullPropertyNames(paramDto));
            }

            // 4. 业务逻辑处理
            Staff staff = staffService.selectStaffById(staffDTO.getId());
            if (staff == null) {
                throw new BusinessException(ErrorCode.UNDEFINED, "用户不存在");
            }
            String password = "";
            // 检查手机号是否已存在
            if (!StringUtils.equals(staff.getMobile(), staffDTO.getMobile())) {
                Staff staff1 = staffService.selectMobileByStaff(staffDTO.getMobile());
                if (AssertUtil.isNotEmpty(staff1)) {
                    throw new BusinessException(ErrorCode.UNDEFINED, "手机号已存在用户！");
                }
            }
            String psw = PasswordUtil.encode("Abc@123456").toLowerCase();
            password = PasswordUtil.encode(staffDTO.getMobile() + psw + staff.getRaw());
            if (!StringUtils.equals(password,"")){
                staff.setPassword(password);
            }
            // 更新用户信息
            BeanUtils.copyProperties(staffDTO, staff);
//            staffService.updateStaff2(staff, paramJson);
            staffService.updateStaff(staff);

            // 返回更新后的用户信息
            return StatusResponse.success(staffService.selectStaffByIdSso(staffDTO.getId()));

        } catch (JsonParseException e) {
            log.error("JSON解析错误: {}", e.getMessage());
            throw new BusinessException(ErrorCode.UNDEFINED, "JSON参数格式错误");
        } catch (JsonMappingException e) {
            log.error("JSON映射错误: {}", e.getMessage());
            throw new BusinessException(ErrorCode.UNDEFINED, "JSON参数映射错误");
        } catch (IOException e) {
            log.error("JSON处理IO错误: {}", e.getMessage());
            throw new BusinessException(ErrorCode.UNDEFINED, "JSON处理错误");
        } catch (Exception e) {
            log.error("更新用户信息异常: {}", e.getMessage());
            throw new BusinessException(ErrorCode.UNDEFINED, "更新用户信息失败");
        }
    }

    // 辅助方法：验证JSON格式
    private boolean isValidJson(String jsonStr) {
        try {
            new ObjectMapper().readTree(jsonStr);
            return true;
        } catch (JsonProcessingException e) {
            return false;
        }
    }

    // 辅助方法：获取空属性名数组
    private String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<>();
        for (PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        return emptyNames.toArray(new String[0]);
    }

    @PostMapping(value = "/update_password", produces = "application/json;charset=utf-8")
    @ResponseBody
    @TokenConfig(value = "1")
    @ApiOperation(value = "修改密码", notes = "修改密码")
    public StatusResponse updatePassword(@RequestHeader String ticket, String oldPassword, String newPassword) {
        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(staffService.updatePassword(redisDetails.getStaffId(), oldPassword, newPassword));
    }

    @PostMapping(value = "/passLogin", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "账号密码登录", notes = "账号密码登录")
    public StatusResponse passLogin(String platformId, String mobile, String password, String ip) {
        return staffService.passLogin(platformId, mobile, password, ip,0);
    }

    @PostMapping(value = "/groupPassLogin", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "集团账号密码登录", notes = "集团账号密码登录")
    public StatusResponse groupPassLogin(String platformId, String mobile, String password, String ip) {
        return staffService.passLogin(platformId, mobile, password, ip,1);
    }

    @PostMapping(value = "/getStaffRole", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "获取用户默认权限", notes = "获取用户默认权限")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffRoleDTO.class),
    })
    @TokenConfig(value = "1")
    public StatusResponse getStaffRole(@RequestHeader String ticket) {
        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(staffService.getStaffRole(redisDetails));
    }


    @PostMapping(value = "/getStaffRoleList", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "获取用户所属系统角色列表", notes = "获取用户所属系统角色列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = RoleDTO.class),
    })
    @TokenConfig(value = "1")
    public StatusResponse getStaffRoleList(@RequestHeader String ticket) {
        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(staffService.getStaffRoleList(redisDetails));
    }

    @PostMapping(value = "/getAreaRoles", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "获取所属系统角色列表和用户数量", notes = "获取所属系统角色列表和用户数量")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = RoleCountDO.class),
    })
    @TokenConfig(value = "1")
    public StatusResponse getAreaRoles(@RequestHeader String ticket, boolean staffCount) {
        RedisDetails redisDetails = redisService.getTicket(ticket);

        Staff staff = staffService.selectStaffById(redisDetails.getStaffId());

        return StatusResponse.success(staffService.getAreaRoles(staff.getAreaId(), true));
    }


    @PostMapping(value = "/setDefaultRole", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "设置用户默认角色岗位", notes = "设置用户默认角色岗位")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffRoleDTO.class),
    })
    @TokenConfig(value = "1")
    public StatusResponse setDefaultRole(@RequestHeader String ticket, String roleId) throws Exception {

        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(staffService.setDefaultRole(redisDetails, roleId));
    }


    @PostMapping(value = "/getDefaultRole", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "获取默认角色岗位", notes = "获取默认角色岗位")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffRoleDTO.class),
    })
    @TokenConfig(value = "1")
    public StatusResponse getDefaultRole(@RequestHeader String ticket) {

        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(staffService.getDefaultRole(redisDetails));
    }

    @PostMapping(value = "/send_sms", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "短信接口", notes = "短信接口")
    public StatusResponse send_sms(String mobile) {
        return StatusResponse.success(staffService.sendSms(mobile));
    }

    @PostMapping(value = "/getAreaDTO", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "获取默认用户园区信息", notes = "获取默认用户园区信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = AreaDTO.class),
    })
    @TokenConfig(value = "1")
    public StatusResponse getAreaDTO(@RequestHeader String ticket) {

        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(staffService.getAreaDTO(redisDetails));
    }

    @PostMapping(value = "/getAreaDetailDTO", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "获取默认用户园区信息", notes = "获取默认用户园区信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = AreaDTO.class),
    })
    @TokenConfig(value = "1")
    public StatusResponse getAreaDetailDTO(@RequestHeader String ticket) {

        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(staffService.getAreaDetailDTO(redisDetails));
    }

    @PostMapping(value = "/getInChargeArea", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "获取已分配的园区集合", notes = "获取已分配的园区集合")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = AreaListDTO.class),
    })
    @TokenConfig(value = "1")
    public StatusResponse getInChargeArea(@RequestHeader String ticket) {

        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(staffService.getInChargeArea(redisDetails));
    }


    @PostMapping(value = "/setDefaultArea", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "设置默认用户园区信息", notes = "设置默认用户园区信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = AreaDTO.class),
    })
    @TokenConfig(value = "1")
    public StatusResponse setDefaultArea(@RequestHeader String ticket, String areaId) {
        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(staffService.setDefaultArea(redisDetails.getStaffId(), areaId));
    }

    @PostMapping(value = "/getNormalStaffIdByArea", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "查询某园所未删除 未离职状态的员工ID", notes = "查询某园所未删除 未离职状态的员工ID")
    @TokenConfig(value = "1")
    public StatusResponse getNormalStaffIdByArea(@RequestHeader String ticket, String areaId) {

        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(staffService.getNormalStaffIdByArea(redisDetails, areaId));
    }


    @PostMapping(value = "/getAreaStaffList", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "查询园区用户列表", notes = "查询园区用户列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffPageDO.class),
    })
    @TokenConfig(value = "1")
    public StatusResponse getAreaStaffList(@RequestHeader String ticket, Integer pageNum, Integer pageSize, String roleId, String realName,String isNotStaffIds,String staffIds) {
        if (pageNum == null || pageNum < 0) {
            pageNum = 0;
        }

        if (pageSize == null) {
            pageSize = 10;
        }
        pageNum = pageNum * pageSize;

        RedisDetails redisDetails = redisService.getTicket(ticket);

        Staff staff = staffService.selectStaffById(redisDetails.getStaffId());

        List<StaffPageDO> list = staffService.getAreaStaffList(staff.getAreaId(), roleId, realName, -1 ,pageNum, pageSize, isNotStaffIds, staffIds);

        return StatusResponse.success(list);
    }


    @PostMapping(value = "/getAreaSetting", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "查询园区字典信息", notes = "查询园区字典信息")
    public StatusResponse getAreaSetting(String label, String type, String areaId) {
        AreaSettingDTO areaSettingDTO = staffService.getAreaSetting(label, type, areaId);
        return StatusResponse.success(areaSettingDTO);
    }

    @PostMapping(value = "/getAllAreaSettings", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "前缀模糊查询园区字典信息", notes = "前缀模糊查询园区字典信息")
    public StatusResponse getAllAreaSettings(String areaId, String prefix) {
        List<AreaSettingDTO> list = staffService.getAllAreaSettings(areaId, prefix);
        return StatusResponse.success(list);
    }


    @PostMapping(value = "/getAllArea", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "查询所有园区", notes = "查询所有园区")
    public StatusResponse getAllArea() {
        List<AreaDTO> list = areaService.getAllArea();
        return StatusResponse.success(list);
    }


    @PostMapping(value = "/addAreaSetting", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "添加园区字典", notes = "添加园区字典")
    public StatusResponse addAreaSetting(String key, String value, String description, String extension, String areaId, String type) {
        Boolean b = dictDataService.addAreaSetting(key, value, description, extension, areaId, type);
        return StatusResponse.success(b);
    }

    @PostMapping(value = "/addAreaSettings", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "批量添加园区字典", notes = "批量添加园区字典")
    public StatusResponse addAreaSettings(String keys, String values, String description, String extension, String areaId, String type) {
        Integer b = dictDataService.addAreaSettings(keys, values, description, extension, areaId, type);
        return StatusResponse.success(b);
    }


    @PostMapping(value = "/getPermissionStaffDTOList", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "查询权限拥有的用户集合", notes = "查询权限拥有的用户集合")
    @TokenConfig(value = "1")
    public StatusResponse getPermissionStaffDTOList(@RequestHeader String ticket, String permission) {

        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(staffService.getPermissionStaffDTOList(redisDetails, permission));
    }

    @PostMapping(value = "/checkStaffPermission", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "查询用户是否拥有的权限集合", notes = "查询用户是否拥有的权限集合")
    @TokenConfig(value = "1")
    public StatusResponse checkStaffPermission(@RequestHeader String ticket, String[] permission) {
        String[] arr = ComUtil.getString(permission);
        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(staffService.checkStaffPermission(redisDetails, arr));
    }

    @PostMapping(value = "/getPermissionInfoV2", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "查询用户权限集合", notes = "查询用户权限集合")
    @TokenConfig(value = "1")
    public StatusResponse getPermissionInfoV2(@RequestHeader String ticket, String platformId) {
        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(staffService.getPermissionInfoV2(redisDetails.getStaffId(), platformId));
    }


    @PostMapping(value = "/getAreaStaffListByPermission", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "根据权限获取园区用户列表", notes = "根据权限获取园区用户列表")
    @TokenConfig(value = "1")
    public StatusResponse getAreaStaffListByPermission(@RequestHeader String ticket, Integer pageNum, Integer pageSize, String permission) {
        if (pageNum == null || pageNum < 0) {
            pageNum = 0;
        }

        if (pageSize == null) {
            pageSize = 100;
        }
        pageNum = pageNum * pageSize;
        RedisDetails redisDetails = redisService.getTicket(ticket);

        List<StaffPageDO> list = staffService.getAreaStaffListByPermission(redisDetails, permission, pageNum, pageSize);
        return StatusResponse.success(list);
    }


    @PostMapping(value = "/getStaffIdByRoleList", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "用户所属系统角色列表", notes = "获取用户所属系统角色列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = RoleDTO.class),
    })
    @TokenConfig(value = "1")
    public StatusResponse getStaffIdByRoleList(@RequestHeader String ticket, String staffId) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        Staff staff = staffService.selectStaffById(redisDetails.getStaffId());
        if (AssertUtil.isEmpty(staff.getAreaId())) {
            throw new BusinessException(ErrorCode.UNDEFINED, "默认园区不存在");
        }
        return StatusResponse.success(staffService.getStaffRoleList(staffId,staff.getAreaId()));
    }

    @PostMapping(value = "/setStaffRoleList", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "批量添加用户角色", notes = "批量添加用户角色")
    @TokenConfig(value = "1")
    public StatusResponse setStaffRoleList(@RequestHeader String ticket, String staffId, String[] roleIds) {
        RedisDetails redisDetails = redisService.getTicket(ticket);

        Staff staff = staffService.selectStaffById(redisDetails.getStaffId());

        List<AreaListDTO> areaList = areaService.getInChargeArea(redisDetails.getStaffId());

        boolean checked = false;
        if (AssertUtil.isEmpty(areaList)) {
            throw new BusinessException(ErrorCode.STAFF_NOT_EXIST, "员工不存在");
        }

        for (AreaListDTO a : areaList) {
            if (a.getAreaId().equals(staff.getAreaId())) {
                checked = true;
                break;
            }
        }

        if (!checked) {
            throw new BusinessException(ErrorCode.STAFF_NOT_EXIST, "员工不存在");
        }

        String[] arr = ComUtil.getString(roleIds);

        roleService.setStaffRoleList(staff.getAreaId(), staffId, arr);

        return StatusResponse.success("成功");
    }

    @PostMapping(value = "/addAreaRole", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "添加或修改默认园区的角色", notes = "添加或修改默认园区的角色")
    @TokenConfig(value = "1")
    public StatusResponse addAreaRole(@RequestHeader String ticket, RoleDTO role) {
        RedisDetails redisDetails = redisService.getTicket(ticket);

        return StatusResponse.success(roleService.addOrUpdateAreaRole(redisDetails, role));
    }

    @PostMapping(value = "/removeAreaRole", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "删除默认园区的角色", notes = "删除默认园区的角色")
    @TokenConfig(value = "1")
    public StatusResponse removeAreaRole(@RequestHeader String ticket, RoleDTO role) {
        roleService.removeAreaRole(role);
        return StatusResponse.success("删除成功");
    }


    @PostMapping(value = "/getTermMenus", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "系统id查-树菜单", notes = "系统id查-树菜单")
    @TokenConfig(value = "1")
    public StatusResponse getTermMenus(@RequestHeader String ticket, String platformId) {
        List<PlatformMenuDTO> list = menuService.getTermMenus(platformId);
        return StatusResponse.success(list);
    }


    @PostMapping(value = "/roleIdAndPlatformIdByTermMenus", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "查询角色id+系统id查-树菜单", notes = "查询角色id+系统id查-树菜单")
    @TokenConfig(value = "1")
    public StatusResponse roleIdAndPlatformIdByTermMenus(@RequestHeader String ticket, String platformId, String roleId) {
        List<Menu> list = menuService.roleIdAndPlatformIdByTermMenus(platformId, roleId);
        return StatusResponse.success(list);
    }


    @PostMapping(value = "/setRoleMenus", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "添加用户 园区 角色菜单关系", notes = "添加用户 园区 角色菜单关系")
    @TokenConfig(value = "1")
    public StatusResponse setRoleMenus(@RequestHeader String ticket, String roleId, String[] menuIds, String platformId) {
        RedisDetails redisDetails = redisService.getTicket(ticket);

        Staff staff = staffService.selectStaffById(redisDetails.getStaffId());

        String[] arr = ComUtil.getString(menuIds);

        Role role = roleService.selectRoleByRoleId(roleId);

        if (!role.getAreaId().equals(staff.getAreaId())) {
            throw new BusinessException(ErrorCode.NOT_ROLE_ERROR, "没有指定的角色");
        }

        RoleDTO roleDTO = menuService.setRoleMenus(roleId, arr, platformId);

        return StatusResponse.success(roleDTO);
    }

    @PostMapping(value = "/createStaff", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "创建用户", notes = "创建用户")
    @ResponseBody
    public StatusResponse createStaff(@RequestHeader String ticket, StaffDTO staffDTO) throws Exception{
        RedisDetails redisDetails = redisService.getTicket(ticket);
        if (AssertUtil.isEmpty(redisDetails)){
            throw new BusinessException(ErrorCode.UNDEFINED, "ticket不存在");
        }

        Staff staff = staffService.selectStaffById(redisDetails.getStaffId());

        staffDTO.setAreaId(staff.getAreaId());

        StaffDTO staffDTO1 = staffService.createStaffDTO(staffDTO);

        return StatusResponse.success(staffDTO1);
    }

    @PostMapping(value = "/createStaff2", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "创建用户", notes = "创建用户")
    @ResponseBody
    public StatusResponse createStaff2(@RequestHeader String ticket,StaffDTO staffDTO,String paramJson) throws Exception{
        RedisDetails redisDetails = redisService.getTicket(ticket);
        if (AssertUtil.isEmpty(redisDetails)){
            throw new BusinessException(ErrorCode.UNDEFINED, "ticket不存在");
        }

        Staff staff = staffService.selectStaffById(redisDetails.getStaffId());

        staffDTO.setAreaId(staff.getAreaId());

        StaffDTO staffDTO1 = staffService.createStaffDTO2(staffDTO,paramJson);

        return StatusResponse.success(staffDTO1);
    }


    @PostMapping(value = "/getStaffGroupByAreaList", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "获取用户所属集团下所有园区", notes = "获取用户所属集团下所有园区")
    @ResponseBody
    @TokenConfig(value = "1")
    public StatusResponse getStaffGroupByAreaList(@RequestHeader String ticket) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        List<AreaDTO> ret = staffService.getStaffGroupByAreaList(redisDetails.getStaffId());
        return StatusResponse.success(ret);
    }

    @PostMapping(value = "/getGroupAll", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "获取用户所属集团下所有园区", notes = "获取用户所属集团下所有园区")
    @ResponseBody
    @TokenConfig(value = "1")
    public StatusResponse getGroupAll(@RequestHeader String ticket) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        List<Area> ret = staffService.getGroupAll(redisDetails.getStaffId());
        return StatusResponse.success(ret);
    }

    @PostMapping(value = "/getGroupStaffList", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "查询集团用户列表", notes = "查询集团用户列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = StaffPageDO.class),
    })
    @TokenConfig(value = "1")
    public StatusResponse getGroupStaffList(@RequestHeader String ticket,
                                            String areaId, String roleId, String realName,
                                            Integer pageNum, Integer pageSize) {
        if (pageNum == null || pageNum < 0) {
            pageNum = 0;
        }

        if (pageSize == null) {
            pageSize = 10;
        }
        int beginIndex = pageNum * pageSize;

        RedisDetails redisDetails = redisService.getTicket(ticket);

        Staff staff = staffService.selectStaffById(redisDetails.getStaffId());

        Area area = areaService.getArea(redisDetails.getStaffId());

        if (staff == null || area == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "用户配置错误");
        }

        List<StaffPageDO> list = staffService.getGroupStaffList(area.getGroupId(), areaId, roleId, realName, -1 ,beginIndex, pageSize);
        Integer total = staffService.getGroupStaffListCount(area.getGroupId(), areaId, roleId, realName, -1);

        return StatusResponse.success(new PageImpl<>(list, PageRequest.of(pageNum, pageSize), total));
    }

    @PostMapping(value = "/getStaffNameAndAreaName", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询用户名称和园区名称", notes = "查询用户名称和园区名称")
    @ResponseBody
    public StatusResponse getStaffNameAndAreaName(String staffId, String areaId) {
        return StatusResponse.success(staffService.getStaffNameAndAreaName(staffId, areaId));
    }

    @PostMapping(value = "/quitStaff", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "员工离职", notes = "员工离职")
    @ResponseBody
    @TokenConfig(value = "1")
    public StatusResponse quitStaff(@RequestHeader String ticket, String areaId, String staffId) {
        RedisDetails redisDetails = redisService.getTicket(ticket);

        if (StringUtils.isEmpty(areaId)) {
            Staff staff = staffService.selectStaffById(redisDetails.getStaffId());
            areaId = staff.getAreaId();
        }

        //离职
        StaffDTO staffDTO = staffService.updateStaffJobStatus(areaId, staffId,1);
        return StatusResponse.success(staffDTO);
    }

    @PostMapping(value = "/staffEntry", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "员工入职", notes = "员工入职")
    @ResponseBody
    @TokenConfig(value = "1")
    public StatusResponse staffEntry(@RequestHeader String ticket, String areaId, String staffId) {
        RedisDetails redisDetails = redisService.getTicket(ticket);

        if (StringUtils.isEmpty(areaId)) {
            Staff staff = staffService.selectStaffById(redisDetails.getStaffId());
            areaId = staff.getAreaId();
        }

        //0入职
        StaffDTO staffDTO = staffService.updateStaffJobStatus(areaId, staffId,0);
        return StatusResponse.success(staffDTO);
    }



    @PostMapping(value = "/getStaffDTOList", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "园区所有员工", notes = "园区所有员工")
    @ResponseBody
    public StatusResponse getStaffDTOList(String areaId) {
        List<StaffDTO> list = staffService.getStaffDTOList(areaId);
        return StatusResponse.success(list);
    }

    @PostMapping(value = "/getAreaStaffs", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "获取分页园区员工", notes = "获取分页园区员工")
    @ResponseBody
    @TokenConfig(value = "1")
    public StatusResponse getAreaStaffs(@RequestHeader String ticket, Integer pageNum, Integer pageSize,
                                        String roleId, String realName,Integer status,String isNotStaffIds,String staffIds) {
        int num = pageNum+1;
        if (pageNum == null || pageNum < 0) {
            pageNum = 0;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        Integer pageNum1 = (num - 1) * pageSize;
//        Integer pageNum1 =  pageSize;
        Integer pageSize1 = num * pageSize;


        RedisDetails redisDetails = redisService.getTicket(ticket);

        Staff staff = staffService.selectStaffById(redisDetails.getStaffId());

        List<StaffPageDO> list = staffService.getAreaStaffList(staff.getAreaId(), roleId, realName,status, pageNum1, pageSize, isNotStaffIds, staffIds);

        Integer total = staffService.getAreaStaffListCount(staff.getAreaId(), roleId, realName, status, pageNum1, pageSize1, isNotStaffIds, staffIds);

        PageImpl<StaffPageDO> page = new PageImpl(list, PageRequest.of(pageNum, pageSize), total);
        if(null!=page.getContent() && page.getContent().size()>0){
            for (StaffPageDO staffPageDO : page.getContent()) {
                staffPageDO.setStatusName(staffPageDO.getStatus()==0?"在职":"离职");

                List<String> classesNameList = new LinkedList<>();
                List<StaffChild> staffChildList = staffChildMapper.childList(staffPageDO.getId());
                if(null!=staffChildList && staffChildList.size()>0){
                    for (StaffChild staffChild : staffChildList) {
                        if(null!=staffChild.getClassesName()){
                            classesNameList.add(staffChild.getClassesName());
                        }
                    }
                }
                staffPageDO.setClassesName(String.join(",",classesNameList));
            }
        }

        return StatusResponse.success(page);
    }

    @PostMapping(value = "/areasByLocation", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "获取经纬度范围内的园区", notes = "获取分页园区员工")
    @ResponseBody
    public StatusResponse areasByLocation(float lat, float lon, Integer num) {
        List<AreaDTO>  list = areaService.areasByLocation(lat,lon,num);
        return StatusResponse.success(list);
    }

    @PostMapping(value = "/areasByLocationLike", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "获取地址模糊查询园区", notes = "获取地址模糊查询园区")
    @ResponseBody
    public StatusResponse areasByLocationLike(String pcaCode, String pcaName) {
        List<AreaDTO>  list = areaService.areasByLocationLike(pcaCode,pcaName);
        return StatusResponse.success(list);
    }

    @PostMapping(value = "/getStaffAreaList", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询集团下的用户关系", notes = "查询集团下的用户关系")
    @ResponseBody
    public StatusResponse getStaffAreaList(@RequestHeader String ticket) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        List<AreaDTO> ret = staffService.getStaffGroupByAreaList(redisDetails.getStaffId());
        List<String> ids = ret.stream().map(k->k.getId()).collect(Collectors.toList());
        List<AreaStaff> list = areaStaffService.selectInAreaId(ids);
        return StatusResponse.success(list);
    }


    @PostMapping(value = "/isData", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询是否有字典", notes = "查询是否有字典")
    @ResponseBody
    @TokenConfig(value = "1")
    public StatusResponse isData(@RequestHeader String ticket,DictData dictData) {
        boolean b = dictDataService.isData(dictData);
        return StatusResponse.success(b);
    }

    @PostMapping(value = "/isDataList", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询是否有字典", notes = "查询是否有字典")
    @ResponseBody
    @TokenConfig(value = "1")
    public StatusResponse isData(@RequestHeader String ticket,String dictData) {
        List<DictData> list = JSONObject.parseArray(dictData, DictData.class);
        boolean b = dictDataService.isDataList(list);
        return StatusResponse.success(b);
    }

    @PostMapping(value = "/staffInfoList", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询多教师基本信息", notes = "查询多教师基本信息")
    @ResponseBody
    @TokenConfig(value = "1")
    public StatusResponse staffInfoDTO(@RequestHeader String ticket,String[] staffIds) {
        RedisDetails redisDetails = redisService.getTicket(ticket);

        AreaDTO areaDTO = staffService.getAreaDTO(redisDetails);
        String[] arr = ComUtil.getString(staffIds);

        List<StaffInfoDTO>  list =  staffService.getStaffInfoList(areaDTO.getId(),arr);

        return StatusResponse.success(list);
    }


    @PostMapping(value = "/staffTeacherInfoList", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询多教师基本信息", notes = "查询多教师基本信息")
    @ResponseBody
    public StatusResponse staffInfoDTO1(@RequestBody AreaStaff areaStaff) {

        Role role = roleService.selectRoleByAreaId(areaStaff.getAreaId());

        List<AreaStaff> areaStaffList = areaStaffService
                .selectInAreaIdAndRoleId(areaStaff.getAreaId(),role.getRoleId());
        List<String> staffIdList = areaStaffList.stream()
                .map(AreaStaff::getStaffId) // 假设AreaStaff类有一个getStaffId()方法
                .collect(Collectors.toList());

        // 将List转换为数组
        String[] staffIdArray = staffIdList.toArray(new String[0]);

        List<StaffInfoDTO>  list =  staffService.getStaffInfoList(areaStaff.getAreaId(),staffIdArray);

        return StatusResponse.success(list);
    }


    @PostMapping(value = "/getStaffGroupList", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询用户的集团集合", notes = "查询用户的集团集合")
    @ResponseBody
    @TokenConfig(value = "1")
    public StatusResponse getStaffGroupList(@RequestHeader String ticket) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        List<Group> list = groupService.selectStaffIdByGroupList(redisDetails.getStaffId());
        return StatusResponse.success(list);
    }

    @PostMapping(value = "/getAreaName", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "查询名称园区", notes = "查询名称园区")
    public StatusResponse getAreaName(String name) {
        List<AreaDTO> list = areaService.getAreaName(name);
        return StatusResponse.success(list);
    }

    @PostMapping(value = "/getAreaGroupAdmin", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "查询是否园区管理员", notes = "查询是否园区管理员")
    public StatusResponse getAreaGroupAdmin(@RequestHeader String ticket) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        AreaDTO areaDTO = staffService.getAreaDTO(redisDetails);
        AreaVO areaVO = areaService.selectAreaById(areaDTO.getId());
        GroupUser groupUser = groupUserMapper.selectGroupUserByGroupId(areaVO.getGroupId(),redisDetails.getStaffId());
        if (Objects.nonNull(groupUser)){
            return StatusResponse.success(groupUser.getGroupId());
        }else {
            return StatusResponse.success("");
        }
    }

    @PostMapping(value = "/getAreaGroupId", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "返回集团ID", notes = "返回集团ID")
    public StatusResponse getAreaGroupId(@RequestHeader String ticket) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        AreaDTO areaDTO = staffService.getAreaDTO(redisDetails);
        AreaVO areaVO = areaService.selectAreaById(areaDTO.getId());
        return StatusResponse.success(areaVO.getGroupId());
    }


    @PostMapping(value = "/getEditionCode", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "获取高版本APP", notes = "获取高版本APP")
    public StatusResponse getEditionCode(String appId,String appVersion,Integer type,String wgtVersion,String isMandatory) {
        AppEdition appEdition = appEditionService.getEditionCode(appId,appVersion,type,wgtVersion);
        return StatusResponse.success(appEdition);
    }

    @ResponseBody
    @PostMapping(value = "/getTeacherByClassId", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "获取班级下教师ids", notes = "获取班级下教师ids")
    public StatusResponse getTeacherByClassId(String classId) {
        List<TeacherInfoDTO> teacherInfoDTOList = staffChildMapper.getTeacherByClassId(classId);

        return StatusResponse.success(teacherInfoDTOList);
    }

}
